package com.cloud.business.controller.sys;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.business.controller.BaseController;
import com.cloud.business.entity.sys.SysSellerUser;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysselleruservm.SysSellerUserModel;
import com.cloud.business.model.sys.sysselleruservm.SysSellerUserPost;
import com.cloud.business.model.sys.sysselleruservm.SysSellerUserQueryModel;
import com.cloud.business.service.sys.ISysSellerUserService;
import com.cloud.core.utils.CustomExcelUtils;
import org.apache.http.HttpResponse;
import org.springframework.web.bind.annotation.*;

import com.cloud.core.common.HttpContentResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户API接口层
 * Copyright: Copyright (C) 2020 DLANGEL, Inc. All rights reserved.
 * Company: 大连安琪科技有限公司
 *
 * @author Rex
 * @since 2021-05-16 14:22:52
 */
@Api(tags = "系统用户模块")
@RestController
@RequestMapping("/api/sysSellerUser")
public class SysSellerUserController extends BaseController {

    @Resource
    private ISysSellerUserService sysSellerUserService;

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param queryModel 参数
     * @return {@link HttpContentResult}
     * @author Rex
     */
    @ApiOperation(value = "获取分页列表")
    @PostMapping("/getSysSellerUserListByPage")
    public HttpContentResult getSysSellerUserListByPage(@RequestBody SysSellerUserQueryModel queryModel) {
        ResultMsg msg = new ResultMsg();
        try {
            queryModel.setOperationId(getOperationId());
            IPage<SysSellerUserModel> page = new Page<>(queryModel.getPageNum(), queryModel.getRecordNum());
            sysSellerUserService.getSysSellerUserListByPage(queryModel, page, msg);
            return HttpContentResult.successResult(msg.getMsg(), page.getTotal(), page.getRecords());
        } catch (Exception ex) {
            return HttpContentResult.errorResult(ex.getMessage());
        }
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param post 入参
     * @return {@link HttpContentResult}
     * @author Rex
     */
    @ApiOperation(value = "创建")
    @PostMapping("/create")
    public HttpContentResult create(@RequestBody SysSellerUserPost post) {
        post.setCreateUserId(getOperationId());
        String msg = sysSellerUserService.create(post);
        return HttpContentResult.successResult(msg, 1L, "");
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param post 入参
     * @return {@link HttpContentResult}
     * @author Rex
     */
    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public HttpContentResult update(@RequestBody SysSellerUserPost post) {
        post.setModificationUserId(getOperationId());
        String msg = sysSellerUserService.update(post);
        return HttpContentResult.successResult(msg, 1L, "");
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param objectId 主键
     * @return {@link HttpContentResult}
     * @author Rex
     */
    @ApiOperation(value = "获取单条")
    @GetMapping("/getById")
    public HttpContentResult getById(Long objectId) {
        SysSellerUser model = sysSellerUserService.getById(objectId);
        return HttpContentResult.successResult("获取成功", 1L, model);
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost 主键集合
     * @return {@link HttpContentResult}
     * @author Rex
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/deleteByList")
    public HttpContentResult deleteByList(@RequestBody DeletePost deletePost) {
        String msg = sysSellerUserService.deleteByList(deletePost);
        return HttpContentResult.successResult(msg, 1L, "");
    }
    //endregion

    //region 获取单条Model

    /**
     * 获取单条Model
     *
     * @param objectId 主键集合
     * @return {@link HttpContentResult}
     * @author Rex
     */
    @ApiOperation(value = "获取单条Model")
    @GetMapping("/getModelById")
    public HttpContentResult getModelById(Long objectId) {
        SysSellerUser model = sysSellerUserService.getById(objectId);
        return HttpContentResult.successResult("获取成功", 1L, model);
    }
    //endregion

    //region 导入数据

    /**
     * 导入数据
     *
     * @param file
     * @return
     * @author Rex
     */
    @RequestMapping(value = "/readExcel", method = RequestMethod.POST)
    public void readExcel(@RequestParam(value = "uploadFile", required = false) MultipartFile file) {
        List<SysSellerUser> list = CustomExcelUtils.readExcel("", SysSellerUser.class, 1, file);
        sysSellerUserService.createBatch(list);
    }
    // endregion

    //region 导出数据
    /**
     * 导出
     *
     * @param
     * @return
     * @author Rex
     */
    @ApiOperation(value = "导出")
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody SysSellerUserQueryModel queryModel, HttpServletResponse response) throws Exception {
        List<SysSellerUser> resultList = sysSellerUserService.getSysSellerUserListForExport(queryModel);
        CustomExcelUtils.writeExcel(response, resultList, SysSellerUser.class);
    }
    // endregion
}
